package cn.yangliu.kindeditor.config;

import cn.yangliu.kindeditor.utils.PropertiesUtils;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 作者 杨柳
 * 时间 2017-09-24 15:31
 * 描述
 */
public class KindEditorConfig {

    //全局访问前缀
    public static String globalUrlPreffix = "/kindeditorShow";
    //保存路径前缀
    public static String globalSavePathPreffix = "/kindeditor";
    //媒体文件
    public static String mediaAllow = "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb,mp4";
    //附件
    public static String fileAllow = "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2";
    //flash
    public static String flashAllow = "swf,flv";
    //图片
    public static String imageAllow = "gif,jpg,jpeg,png,bmp";
    //文件上传统一最大限制
    public static String maxSize = "52428800";

    public static String configPath = "META-INF/kindeditor-defualt.properties";


    private static Map<String,String> allowMap;
    private static Map<String,String> ohterConfigMap;

    public static void init() {
        try {
            if (configPath != null && !"".equals(configPath)) {
                InputStream inputStream ;
                if (configPath.startsWith("/")){
                    inputStream = new FileInputStream(configPath);
                }else {
                    inputStream = KindEditorConfig.class.getClassLoader().getResourceAsStream(configPath);
                }
                Properties properties = PropertiesUtils.load(inputStream);
                globalUrlPreffix = properties.getProperty("global-url-preffix", globalUrlPreffix);
                globalSavePathPreffix = properties.getProperty("global-save-path-preffix", globalSavePathPreffix);
                mediaAllow = properties.getProperty("media-allow", mediaAllow);
                fileAllow = properties.getProperty("file-allow", fileAllow);
                flashAllow = properties.getProperty("flash-allow", flashAllow);
                imageAllow = properties.getProperty("image-allow", imageAllow);
                maxSize = properties.getProperty("max-size", maxSize);
            }

            allowMap = new HashMap<>();
            allowMap.put("media", mediaAllow);
            allowMap.put("file", fileAllow);
            allowMap.put("flash", flashAllow);
            allowMap.put("image", imageAllow);

            ohterConfigMap = new HashMap<>();
            ohterConfigMap.put("globalUrlPreffix", globalUrlPreffix);
            ohterConfigMap.put("globalSavePathPreffix", globalSavePathPreffix);
            ohterConfigMap.put("maxSize", maxSize);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }

    }

    public static Map<String,String> getAllowMap(){
        if (allowMap==null){
            init();
        }
        return allowMap;
    }

    public static Map<String,String> getOtherConfigMap(){
        if (ohterConfigMap==null){
            init();
        }
        return ohterConfigMap;
    }

}
